#!/bin/sh

VERSION="0_8_7_alpha"

SYNTAX="Usage: $0 [--patch <base rev>]"

# Source paths
BUILDDIR=build
PLUGINFILE=thc_rpg.smx
PLUGINFILES="cstrike/*"
SOURCEDIR="src/*"

# Destination paths
RELEASEDIR=release
SOURCE_DEST=$RELEASEDIR/addons/sourcemod/scripting
PLUGINDIR=$RELEASEDIR/addons/sourcemod/plugins
ZIPFILE=thc_rpg_v$VERSION.zip

MAKEPATCH=false

# Clean build and release directory.
make clean
rm -rf $RELEASEDIR
echo "Cleaned build and release directory."

# Exit if cleaning only.
if [ "$1" = "--clean" ]
then
    exit 0
fi

# Make release directory.
mkdir -p $RELEASEDIR

# Compile plugin.
make

# Check if the plugin is built.
if [ ! -e $BUILDDIR/$PLUGINFILE ]
then
    echo "Cannot build release package, plugin build failed. Missing file $BUILDDIR/$PLUGINFILE."
    exit 1
fi

# Rebuild hgversion.h.inc for unofficial builds.
sh updateversion.sh --unofficial

# Copy all files.
echo "Copying plugin files..."
cp -r $PLUGINFILES $RELEASEDIR

# Copy files.
echo "Copying plugin binary..."
mkdir -p $PLUGINDIR
cp -r $BUILDDIR/$PLUGINFILE $PLUGINDIR/$PLUGINFILE

echo "Copying plugin source code..."
mkdir -p $SOURCE_DEST
cp -r $SOURCEDIR $SOURCE_DEST

# Make release package.
echo "Compressing files..."
cd $RELEASEDIR
zip -r $ZIPFILE *

echo "Release package available at $RELEASEDIR/$ZIPFILE."
